package com.thoughtworks.wechatmoments.logic.moments;

import com.thoughtworks.commonlibrary.compat.rxjava.RxSchedulers;
import com.thoughtworks.wechatmoments.base.BasePresenter;
import com.thoughtworks.wechatmoments.Config;
import com.thoughtworks.wechatmoments.entity.TweetsEntity;
import com.thoughtworks.wechatmoments.entity.UserInfoEntity;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * Moments的Presenter
 *
 * @author guowenlong
 * 创建时间:2018-12-07-0:01
 */
public class MomentsPresenter extends BasePresenter<MomentsContract.View> implements MomentsContract.Presenter {

    private MomentsContract.Model mModel;
    private List<TweetsEntity> mTweetsEntities;

    MomentsPresenter() {
        mModel = new MomentsModel();
    }

    @Override
    public void getUserInfo() {
        mModel.getUserInfo().compose(RxSchedulers.ioToMain()).subscribe(new Observer<UserInfoEntity>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(UserInfoEntity userInfoEntity) {
                if (userInfoEntity == null) return;
                mView.showUserInfo(userInfoEntity);
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        });
    }

    private List<TweetsEntity> filterEmptyTweet(List<TweetsEntity> tweetsEntitys) {
        ArrayList<TweetsEntity> filteredList = new ArrayList<>();
        boolean isEmpty;
        for (TweetsEntity tweetsEntity : tweetsEntitys) {
            isEmpty = !(tweetsEntity.getContent() == null && (tweetsEntity.getImages() == null || tweetsEntity.getImages().size() == 0));
            tweetsEntity.setType(MomentsAdapter.TYPE_TWEETS);
            if (isEmpty) filteredList.add(tweetsEntity);
        }
        return filteredList;
    }

    @Override
    public void getUserTweetsOneTime() {
        mModel.getUserTweetsOneTime().compose(RxSchedulers.ioToMain()).subscribe(new Observer<List<TweetsEntity>>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(List<TweetsEntity> tweetsEntities) {
                if (tweetsEntities != null) {
                    mTweetsEntities = filterEmptyTweet(tweetsEntities);
                    getUserTweets();
                }
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        });
    }

    @Override
    public void getUserTweets() {
        mView.showTweets(getTweets(Config.TWEETS_INDEX, Config.TWEETS_LIMIT));
    }

    @Override
    public void getMoreTweets(int index, int limit) {
        mView.showMoreTweets(getTweets(index, limit));
    }

    private List<TweetsEntity> getTweets(int index, int limit) {
        if (mTweetsEntities == null) return new ArrayList<>();
        if (index + limit > mTweetsEntities.size()) return new ArrayList<>();
        ArrayList<TweetsEntity> tweetsEntities = new ArrayList<>();
        for (int i = 0, j = mTweetsEntities.size(); i < j; i++) {
            if (i >= index && i < index + limit) {
                tweetsEntities.add(mTweetsEntities.get(i));
            }
        }
        return tweetsEntities;
    }
}